package com.njlime.cms.repository.app;

import com.njlime.cms.entities.Information;
import com.njlime.core.base.repository.impl.BaseDaoImpl;
import com.njlime.core.helper.PageBean;
import com.njlime.core.utils.StringUtil;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by Dean on 2017-10-09.
 */
@Repository
public class AppInformationDaoImpl extends BaseDaoImpl<Information> {

    /**
     * 获取详情
     *
     * @param id
     * @return
     */
    public Map<String, Object> get(int id, Integer unit_id) {
        StringBuilder sb = new StringBuilder();
        List<Object> arrList = new ArrayList<Object>();
        sb.append(" SELECT ti.*, tu.unit_name unit_name,tu.head_url unit_img FROM t_information ti ");
        //sb.append("LEFT JOIN t_label tl ON tl.id = ti.label_id ");
        sb.append("JOIN t_unit tu ON tu.id = ti.unit_id ");
        sb.append(" WHERE ti.id = ? ");
        arrList.add(id);
        /*if (unit_id != null && unit_id.intValue() != 0) {
            sb.append(" and unit_id = ? ");
            arrList.add(unit_id);
        }*/
        List<Map<String, Object>> re = getListJdbc(sb.toString(), arrList);
        if (re != null && re.size() > 0) {
            return re.get(0);
        }
        return null;
    }

    /**
     * 获取列表
     *
     * @param pageBean
     * @return
     */
    public PageBean list(PageBean pageBean, Information information, int root_unit_id) {
        StringBuilder sb = new StringBuilder();
        List<Object> arrList = new ArrayList<Object>();
        sb.append(" SELECT tin.*,tl.name name" +
                " FROM " +
                " t_information tin " +
                " LEFT JOIN t_label tl ON tl.id = tin.label_id " +
                " where 1=1 and tin.is_del = 0 ");
        sb.append(" and tin.root_unit_id = ? ");
        arrList.add(root_unit_id);
        if (information.getUnit_id() != null && information.getUnit_id().intValue() != 0) {
            sb.append(" and tin.unit_id = ? ");
            arrList.add(information.getUnit_id());
        }
        if(information.getChannel_id() != null){
            sb.append(" and tin.channel_id = ? ");
            arrList.add(information.getChannel_id());
        }
        if(information.getColumn_id() != null){
            sb.append(" and tin.column_id = ? ");
            arrList.add(information.getColumn_id());
        }
        if (information.getLabel_id() != null) {
            sb.append(" and tin.label_id = ? ");
            arrList.add(information.getLabel_id());
        }
        if (information.getPublish_status() != null) {
            sb.append(" and tin.publish_status = ? ");
            arrList.add(information.getPublish_status());
        }
        if (information.getPosition() != null) {
            sb.append(" and tin.position = ? ");
            arrList.add(information.getPosition());
        }
        if (!StringUtil.isEmpty(information.getTitle())) {
            sb.append(" and tin.title like concat('%',?,'%') ");
            arrList.add(information.getTitle());
        }
        if (!StringUtil.isEmpty(information.getStartTime())) {
            sb.append(" and tin.create_time > ? ");
            arrList.add(information.getStartTime());
        }
        if (!StringUtil.isEmpty(information.getEndTime())) {
            sb.append(" and tin.create_time < ? ");
            arrList.add(information.getEndTime());
        }
        int num = getListJdbcSize(sb.toString(), arrList);
        pageBean.setTotalCount(num);
        if (num > 0) {
            sb.append(" order by tin.id desc limit ?,?");
            arrList.add((pageBean.getCurPage() - 1) * pageBean.getPageSize());
            arrList.add(pageBean.getPageSize());
            pageBean.setMapList(getListJdbc(sb.toString(), arrList));
        }
        return pageBean;
    }

    /**
     * 改变阅读数
     *
     * @return
     */
    public int updateReading(int id, Integer unit_id) {
        StringBuilder sb = new StringBuilder();
        List<Object> arrList = new ArrayList<Object>();
        sb.append(" update t_information set reading = IFNULL(reading,0) + 1 where id = ?");
        arrList.add(id);
        if (unit_id != null) {
            sb.append(" and unit_id = ? ");
            arrList.add(unit_id);
        }
        return updateJdbc(sb.toString(), arrList);
    }

    /**
     * banner图列表
     *
     * @return
     */
    public List<Map<String, Object>> bannerList(Information information, int root_unit_id) {
        StringBuilder sb = new StringBuilder();
        List<Object> arrList = new ArrayList<Object>();
        sb.append("select tin.* FROM  t_information tin " +
                "where root_unit_id = ? " +
                "and tin.position in (2,3,4) and tin.publish_status = 3 and tin.is_del = 0 and type = 1 ");
        arrList.add(root_unit_id);
        if(information.getChannel_id() != null){
            sb.append(" and tin.channel_id = ? ");
            arrList.add(information.getChannel_id());
        }
        if(information.getColumn_id() != null){
            sb.append(" and tin.column_id = ? ");
            arrList.add(information.getColumn_id());
        }
        sb.append(" order by tin.position asc,tin.create_time desc");
        List<Map<String, Object>> re = getListJdbc(sb.toString(), arrList);
        return re;
    }

    /**
     * 置顶列表
     *
     * @return
     */
    public List<Map<String, Object>> topList(Information information, int root_unit_id) {
        StringBuilder sb = new StringBuilder();
        List<Object> arrList = new ArrayList<Object>();
        sb.append("select tin.*，tl.name name FROM  t_information tin " +
                " JOIN t_label tl ON tl.id = tin.label_id " +
                " where root_unit_id = ? " +
                " and position = 1 and publish_status= 3 and tin.is_del = 0 and type = 1  ");
        arrList.add(root_unit_id);
        if(information.getChannel_id() != null){
            sb.append(" and tin.channel_id = ? ");
            arrList.add(information.getChannel_id());
        }
        if(information.getColumn_id() != null){
            sb.append(" and tin.column_id = ? ");
            arrList.add(information.getColumn_id());
        }
        sb.append(" order by id desc");
        List<Map<String, Object>> re = getListJdbc(sb.toString(), arrList);
        return re;
    }

    /**
     * 非banner图列表（包含置顶）
     *
     * @param pageBean
     * @param user_id
     * @return
     */
    public PageBean infoList(PageBean pageBean, Information information, Integer user_id, int root_unit_id) {
        StringBuilder sb = new StringBuilder();
        List<Object> arrList = new ArrayList<Object>();
        sb.append("select tin.*, tl.name name FROM t_information tin " +
                " JOIN t_label tl ON tl.id = tin.label_id " +
                " where tin.position in (1,99) and tin.publish_status= 3 and tin.is_del = 0  and type = 1 ");
        sb.append(" and tin.root_unit_id = ? ");
        arrList.add(root_unit_id);
        if(information.getChannel_id() != null){
            sb.append(" and tin.channel_id = ? ");
            arrList.add(information.getChannel_id());
        }
        if(information.getColumn_id() != null){
            sb.append(" and tin.column_id = ? ");
            arrList.add(information.getColumn_id());
        }
        if(information.getTopic_id() != null){
            sb.append(" and tin.topic_id = ? ");
            arrList.add(information.getTopic_id());
        }else{
            sb.append(" and tin.topic_id = 0 ");
        }
        int num = getListJdbcSize(sb.toString(), arrList);
        pageBean.setTotalCount(num);
        if (num > 0) {
            sb.append(" order by tin.position asc, tin.create_time desc ");
            if (user_id != null) {
                /*sb.append(",instr((select tags from t_member where id = ?), label_id) desc");
                arrList.add(user_id);*/
            }
            sb.append(" limit ?,?");
            arrList.add((pageBean.getCurPage() - 1) * pageBean.getPageSize());
            arrList.add(pageBean.getPageSize());
            pageBean.setMapList(getListJdbc(sb.toString(), arrList));
        }
        return pageBean;
    }


    /**
     * 专题列表
     *
     * @param pageBean
     * @return
     */
    public PageBean topicList(PageBean pageBean, Information information, int root_unit_id) {
        StringBuilder sb = new StringBuilder();
        List<Object> arrList = new ArrayList<Object>();
        sb.append("select tin.*, tl.name name FROM t_information tin " +
                " JOIN t_label tl ON tl.id = tin.label_id " +
                " where tin.publish_status= 3 and tin.is_del = 0 and type = 2 ");
        sb.append(" and tin.root_unit_id = ? ");
        arrList.add(root_unit_id);
        if(information.getChannel_id() != null){
            sb.append(" and tin.channel_id = ? ");
            arrList.add(information.getChannel_id());
        }
        if(information.getColumn_id() != null){
            sb.append(" and tin.column_id = ? ");
            arrList.add(information.getColumn_id());
        }
        int num = getListJdbcSize(sb.toString(), arrList);
        pageBean.setTotalCount(num);
        if (num > 0) {
            sb.append(" order by tin.position asc, tin.create_time desc ");
            sb.append(" limit ?,?");
            arrList.add((pageBean.getCurPage() - 1) * pageBean.getPageSize());
            arrList.add(pageBean.getPageSize());
            pageBean.setMapList(getListJdbc(sb.toString(), arrList));
        }
        return pageBean;
    }

    /**
     * 用户已购买的单篇
     * @param pageBean
     * @param user_id
     * @param root_unit_id
     * @return
     */
    public PageBean singleList(PageBean pageBean, int user_id, int root_unit_id){
        StringBuilder sb = new StringBuilder();
        List<Object> arrList = new ArrayList<Object>();
        sb.append("select info.*,tl.name from t_order o ");
        sb.append("join t_information info on info.id = o.busi_id ");
        sb.append("JOIN t_label tl ON tl.id = info.label_id ");
        sb.append("where o.user_id = ? and o.order_type = 3 and status = 2 and info.root_unit_id = ? ");
        arrList.add(user_id);
        arrList.add(root_unit_id);
        int num = getListJdbcSize(sb.toString(), arrList);
        pageBean.setTotalCount(num);
        if (num > 0) {
            sb.append(" order by o.create_time desc ");
            sb.append(" limit ?,?");
            arrList.add((pageBean.getCurPage() - 1) * pageBean.getPageSize());
            arrList.add(pageBean.getPageSize());
            pageBean.setMapList(getListJdbc(sb.toString(), arrList));
        }
        return pageBean;
    }

    /**
     * 用户收藏列表
     * @param pageBean
     * @param user_id
     * @param root_unit_id
     * @return
     */
    public PageBean collectList(PageBean pageBean, int user_id, int root_unit_id){
        StringBuilder sb = new StringBuilder();
        List<Object> arrList = new ArrayList<Object>();
        sb.append("select info.*,tl.name,c.id collect_id from t_member_collection c ");
        sb.append("join t_information info on info.id = c.business_id ");
        sb.append("JOIN t_label tl ON tl.id = info.label_id ");
        sb.append("where c.business_type = 1 and c.member_id = ? and info.root_unit_id = ? ");
        arrList.add(user_id);
        arrList.add(root_unit_id);
        int num = getListJdbcSize(sb.toString(), arrList);
        pageBean.setTotalCount(num);
        if (num > 0) {
            sb.append("order by c.create_time desc limit ?,?");
            arrList.add((pageBean.getCurPage() - 1) * pageBean.getPageSize());
            arrList.add(pageBean.getPageSize());
            pageBean.setMapList(getListJdbc(sb.toString(), arrList));
        }
        return pageBean;
    }

    /**
     * 用户历史列表
     * @param pageBean
     * @param user_id
     * @param root_unit_id
     * @return
     */
    public PageBean historyList(PageBean pageBean, int user_id, int root_unit_id){
        StringBuilder sb = new StringBuilder();
        List<Object> arrList = new ArrayList<Object>();
        sb.append("select info.*,tl.name,t.id track_id from t_member_track t ");
        sb.append("join t_information info on info.id = t.business_id ");
        sb.append("JOIN t_label tl ON tl.id = info.label_id ");
        sb.append("where t.business_type = 1 and t.member_id = ? and info.root_unit_id = ? ");
        arrList.add(user_id);
        arrList.add(root_unit_id);
        int num = getListJdbcSize(sb.toString(), arrList);
        pageBean.setTotalCount(num);
        if (num > 0) {
            sb.append("order by t.create_time desc limit ?,?");
            arrList.add((pageBean.getCurPage() - 1) * pageBean.getPageSize());
            arrList.add(pageBean.getPageSize());
            pageBean.setMapList(getListJdbc(sb.toString(), arrList));
        }
        return pageBean;
    }

    /**
     * 增加评论数
     *
     * @param informationId
     */
    public int updateCommentNum(Integer informationId, int num) {
        StringBuilder sb = new StringBuilder();
        List<Object> arrList = new ArrayList<Object>();
        sb.append(" UPDATE t_information SET comment_num = comment_num + ? WHERE id = ?");
        arrList.add(num);
        arrList.add(informationId);
        return updateJdbc(sb.toString(), arrList);
    }

    /**
     * 更新情绪
     *
     * @param informationId
     */
    public int updateMotion(Integer informationId, String motion) {
        StringBuilder sb = new StringBuilder();
        List<Object> arrList = new ArrayList<Object>();
        sb.append(" UPDATE t_information SET motion = ? WHERE id = ?");
        arrList.add(motion);
        arrList.add(informationId);
        return updateJdbc(sb.toString(), arrList);
    }
}
